<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Queue</title>
  </head>
  <body>
    <script>
      class Queue {
        constructor() {
          this.arr = [];
        }
        // 入栈
        enqueue(ele) {
          this.arr.push(ele);
        }
        // 出栈
        dequeue() {
          return this.arr.shift();
        }
        // 返回队列第一个元素
        front() {
          return this.arr[0];
        }
        isEmpty() {
          return this.arr.length === 0;
        }
        size() {
          return this.arr.length;
        }
        toString() {
          var str = "";
          for (var i = 0; i < this.arr.length; i++) {
            str += this.arr[i] + " ";
          }
          return str;
        }
      }

      // let que = new Queue()
      // que.enqueue(1)
      // que.enqueue(2)
      // que.enqueue(3)
      // que.enqueue(4)
      // console.log(que)
      // que.dequeue()
      // console.log(que)
      // console.log(que.front())
      // console.log(que.isEmpty())
      // console.log(que.size())
      // console.log(que.toString())
      // [1,2,3,4,5,6,7]
      // 击鼓传花
      function test(list, num) {
        let que = new Queue();
        for (let i = 0; i < list.length; i++) {
          que.enqueue(list[i]);
        }
        while (que.size() > 1) {
          for (let j = 0; j < num - 1; j++) {
            que.enqueue(que.dequeue());
          }
          que.dequeue();
        }
        return list.indexOf(que.front());
      }
      let index = test([1, 2, 3, 4, 5, 6, 7], 3);
      console.log(index);
    </script>
  </body>
</html>
